package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.dto.PriceDetailDto;
import com.xingkeduo.pojo.dto.PriceManagementDto;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/5/13 14:54
 **/
public class PriceManagementDtoJTO extends BaseJson {

    public static ObjectNode toJson(PriceManagementDto table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {

            SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            root.put("id", String.valueOf(table.getId()));
            root.put("code", table.getCode());
            root.put("name", table.getName() != null ? table.getName() : "");
            root.put("nickName", table.getNickName() != null ? table.getNickName() : "");
            root.put("shopName", table.getShopName());
            root.put("priceInfo", table.getPriceInfo());
            if (null != table.getPriceDto()) {
                root.putPOJO("priceService", BaseDTOJTO.toJson(table.getPriceDto()));
            }
            root.put("shopId", table.getShopId());
            if (null != table.getStatus()) {
                root.put("status", table.getStatus().getValue());
            } else {
                root.put("status", "还未设置价格");
            }
            if (null != table.getUpdateDate()) {
                root.put("updateDate", formatTimestamp.format(table.getUpdateDate()));
            }

            if(null != table.getLeavelType()){
                root.put("leavelType",table.getLeavelType());
            }
        }
        return root;
    }

    public static ArrayNode toJson(List<PriceManagementDto> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<PriceManagementDto> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }


    //调价详情
    public static ObjectNode toJsonDetail(PriceDetailDto table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {
            root.put("id", String.valueOf(table.getCraftsmanId()));
            if (null != table.getShopInfo()) {
                root.putPOJO("shopInfo", BaseDTOJTO.toJson(table.getShopInfo()));
            }
            root.putPOJO("serviceList", BaseDTOJTO.toJson(table.getServiceList()));
        }
        return root;
    }

    public static ArrayNode toJsonDetail(List<PriceDetailDto> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<PriceDetailDto> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJsonDetail(iterator.next()));
            }

        return root;
    }
}
